using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HabilisDesign.Scribbler
{
	public class ClippingCollection : ObservableCollection<Clipping>
	{
		#region Fields

		private IClippingMonitor m_ClippingMonitor;
		private int m_MaxSize = 20;
		
		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ClippingCollection"/> class.
		/// </summary>
		public ClippingCollection()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ClippingCollection"/> class.
		/// </summary>
		/// <param name="clippingMonitor">The clipping monitor.</param>
		public ClippingCollection( IClippingMonitor clippingMonitor )
		{
			m_ClippingMonitor = clippingMonitor;
			m_ClippingMonitor.ClippingArrive += new EventHandler( HandleClippingArrival );
		}

		/// <summary>
		/// Releases unmanaged resources and performs other cleanup operations before the
		/// <see cref="ClippingCollection"/> is reclaimed by garbage collection.
		/// </summary>
		~ClippingCollection()
		{
			if ( m_ClippingMonitor != null )
			{
				m_ClippingMonitor.ClippingArrive -= new EventHandler( HandleClippingArrival );
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Handles the clipping arrival.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void HandleClippingArrival( object sender, EventArgs e )
		{
			// TODO: Need EventArgs override.
			AddClipping( new Clipping() );
		}

		/// <summary>
		/// Adds the clipping.
		/// </summary>
		/// <param name="clipping">The clipping.</param>
		public void AddClipping( Clipping clipping )
		{
			if ( this.Count == m_MaxSize )
			{
				this.RemoveItem( 0 );
			}
			this.Add( clipping );
		}

		/// <summary>
		/// Resizes the collection.
		/// </summary>
		/// <param name="maxSize">Size of the max.</param>
		public void ResizeCollection( int maxSize )
		{
			while ( this.Count > maxSize )
			{
				this.RemoveItem( 0 );
			}
			m_MaxSize = maxSize;
		}

		#endregion

	} // end class

} // end namespace